<template>
	<sw-form-frame v-model="visible" :title="formName" width="560" :type="formType" :loading="btnLoading" footer @submit="handleSubmitForm()">
		<el-form ref="form" :model="formData" :rules="formRules" label-width="100px" size="medium">
			<el-row :gutter="20">
				<el-col :span="24">
					<el-form-item label="上级菜单" prop="pid" label-width="80px">
						<sw-search-tree-select v-model="formData.pid" v-bind="treeSelectConfig" :tree-data="treeListData"></sw-search-tree-select>
					</el-form-item>
				</el-col>
				<el-col :span="24">
					<el-form-item :label="`${labelName}名称`" prop="name" label-width="80px">
						<el-input type="input" v-model="formData.name" maxlength="64" show-word-limit></el-input>
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item :label="`是否业务${labelName}`">
						<el-radio-group v-model="formData.isBusi">
							<el-radio label="1">是</el-radio>
							<el-radio label="2">否</el-radio>
						</el-radio-group>
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item :label="`是否开发${labelName}`">
						<el-radio-group v-model="formData.isDev">
							<el-radio label="1">是</el-radio>
							<el-radio label="2">否</el-radio>
						</el-radio-group>
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item :label="`是否激活${labelName}`">
						<el-radio-group v-model="formData.del">
							<el-radio label="1">是</el-radio>
							<el-radio label="2">否</el-radio>
						</el-radio-group>
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item :label="`是否公共${labelName}`">
						<el-radio-group v-model="formData.isPublic">
							<el-radio label="1">是</el-radio>
							<el-radio label="2">否</el-radio>
						</el-radio-group>
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item :label="`${labelName}排序`" label-width="80px">
						<el-input-number v-model="formData.orderSort" controls-position="right" :min="1" style="width: 150px"></el-input-number>
					</el-form-item>
				</el-col>
				<el-col :span="12" v-if="formData.level !== '3'">
					<el-form-item label="菜单图标">
						<select-icon v-model="formData.icon"></select-icon>
					</el-form-item>
				</el-col>
				<el-col :span="24" v-if="formData.level !== '3'">
					<el-form-item label="菜单路径" prop="url" label-width="80px">
						<el-input type="input" v-model="formData.url"></el-input>
					</el-form-item>
				</el-col>
				<el-col :span="24">
					<el-form-item :label="`${labelName}描述`" prop="content" label-width="80px">
						<el-input type="textarea" v-model="formData.content"></el-input>
					</el-form-item>
				</el-col>
			</el-row>
		</el-form>
	</sw-form-frame>
</template>

<script>
import lodash from 'lodash';
import selectIcon from './selectIcon';
import formMixins from '@/mixins/formMixins';
import { queryMenu, addedMenu, updateMenu } from '@/api/system/tbSysMenu';

export default {
	mixins: [formMixins],
	components: { selectIcon },
	props: {
		list: { type: Array, default: () => [] }
	},
	data() {
		return {
			formName: '菜单',
			formRules: {
				pid: [{ required: true, message: '请选择上级菜单', trigger: 'change' }],
				name: [{ required: true, message: '请输入菜单名称', trigger: 'blur' }],
				url: [{ required: true, message: '请选择字典类型', trigger: 'change' }]
			},
			treeSelectConfig: {
				placeholder: '选择菜单',
				style: { width: '100%' },
				defaultProps: { label: 'name', children: 'children' }
			}
		};
	},
	computed: {
		isDisabled() {
			return this.isSub;
		},
		detailData() {
			return eval(queryMenu);
		},
		callFunction() {
			return eval(this.formType === 'edit' ? updateMenu : addedMenu);
		},
		treeListData() {
			let children = this.list;
			return [{ id: '0', name: '顶级菜单', children }];
		},
		labelName() {
			return this.formData.level === '3' ? '按钮' : '菜单';
		}
	},
	watch: {
		'formData.pid': {
			handler(val) {
				if (!val) return false;
				this.getCheckedFormData(val);
			},
			immediate: true
		}
	},
	methods: {
		//获取选中表单数据
		getCheckedFormData(id) {
			let datas = this.$findNodeInTree(this.list, id);
			if (Object.keys(datas).length === 0) {
				this.$set(this.formData, 'level', '1');
			} else if (datas.pid === '0') {
				this.$set(this.formData, 'level', '2');
			} else {
				this.$set(this.formData, 'level', '3');
			}
		}
	}
};
</script>
<style lang="scss" lang="scss"></style>
